package com.springdoor.wx.api.service.impl;

import com.springdoor.wx.api.entity.ResultConfig;
import com.springdoor.wx.api.entity.ResultSku;
import com.springdoor.wx.api.entity.ResultSkuDetail;
import com.springdoor.wx.api.entity.SprdGoodsShop;
import com.springdoor.wx.api.mapper.SprdGoodsConfigMapper;
import com.springdoor.wx.api.mapper.SprdGoodsDescMapper;
import com.springdoor.wx.api.mapper.SprdGoodsImgMapper;
import com.springdoor.wx.api.mapper.SprdGoodsShopMapper;
import com.springdoor.wx.api.service.SprdGoodsShopService;
import com.springdoor.wx.utils.string.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
public class SprdGoodsShopServiceImpl implements SprdGoodsShopService {

    @Autowired
    private SprdGoodsShopMapper goodsShopMapper;

    @Autowired
    private SprdGoodsImgMapper goodsImgMapper;

    @Autowired
    private SprdGoodsDescMapper goodsDescMapper;
    
    @Autowired
    private SprdGoodsConfigMapper goodsConfigMapper;

    @Override
    public List<ResultSkuDetail> list(SprdGoodsShop goodsShop) {
        List<ResultSkuDetail> list = goodsShopMapper.list(goodsShop);
        if (list != null && list.size() > 0) {
            list.forEach(sku -> {
                genrateSpuTag(sku);
            });
        }
//         Tags
        return list;
    }

    @Override
    public ResultSkuDetail fetchInfoByGShopId(String gShopId) {
        if (StringUtils.isEmpty(gShopId)) {
            return null;
        }
        ResultSkuDetail sku = goodsShopMapper.fetchInfoByGShopId(gShopId);
        if (sku == null) {
            return null;
        }
        String gNumber = sku.getgNumber();
        // 图片
        List<String> images = goodsImgMapper.fetchImgsByGNumber(gNumber, "2");
        List<String> desc = goodsImgMapper.fetchImgsByGNumber(gNumber, "3");
        sku.setImages(images);
        sku.setDesc(desc);

        String etitle = goodsDescMapper.fetchDescByGNumber(gNumber);
        sku.setEtitle(etitle);

        String assNumber = sku.getAssNumber();
        Map<String, Integer> priceMap = goodsShopMapper.fetchAssGoodsPrice(assNumber);
        if (priceMap != null) {
            sku.setMinLinePrice(priceMap.get("minPrice"));
            sku.setMaxLinePrice(priceMap.get("maxPrice"));
        }

        // 商品规格
        generateAllSpec(sku);
        // 商品属性分类
        generateSpecList(sku, gShopId);
        // Tags
        genrateSpuTag(sku);
        return sku;
    }

    @Override
    public ResultSkuDetail fetchSpecInfoBySkuId(String skuId) {
        if (StringUtils.isEmpty(skuId)) {
            return null;
        }
        ResultSkuDetail sku = goodsShopMapper.fetchInfoByGShopId(skuId);
        if (sku == null) {
            return null;
        }
        String gNumber = sku.getgNumber();
        String assNumber = sku.getAssNumber();
        Map<String, Integer> priceMap = goodsShopMapper.fetchAssGoodsPrice(assNumber);
        if (priceMap != null) {
            sku.setMinLinePrice(priceMap.get("minPrice"));
            sku.setMaxLinePrice(priceMap.get("maxPrice"));
        }
        // 商品规格
        generateAllSpec(sku);
        // 商品属性分类
        generateSpecList(sku, skuId);
        return sku;
    }

    private void genrateSpuTag(ResultSkuDetail sku) {
        List<Object> spuTagList = new ArrayList<>();
        Map<String, String> tag = new HashMap<>();
        tag.put("title", "限时抢购");
        spuTagList.add(tag);
        sku.setSpuTagList(spuTagList);
    }

    /**
     * 属性分类
     * @param sku
     */
    private void generateAllSpec(ResultSkuDetail sku) {
        List<ResultConfig> configList = goodsConfigMapper.fetchConfigByGNumber(sku.getgNumber());
        ResultConfig config1 = new ResultConfig();
        config1.setConfigName("商品名称");
        config1.setValueName(sku.getTitle());
        configList.addFirst(config1);
        ResultConfig config2 = new ResultConfig();
        config2.setConfigName("品牌");
        config2.setValueName(sku.getIntro());
        configList.addFirst(config2);
        ResultConfig config3 = new ResultConfig();
        config3.setConfigName("商品编号");
        config3.setValueName(sku.getgNumber());
        configList.addFirst(config3);
        ResultConfig config4 = new ResultConfig();
        config4.setConfigName("店铺");
        config4.setValueName(sku.getStore());
        configList.addFirst(config4);

        sku.setConfigList(configList);
    }

    /**
     * 属性分类
     * @param sku
     */
    private void generateSpecList(ResultSkuDetail sku, String gShopId) {
        String assNumber = sku.getAssNumber();
        if (StringUtils.isEmpty(assNumber)) {
            assNumber = sku.getgNumber();
        }
        String assort = sku.getAssort();
        String[] split = StringUtils.split(assort, ",");
        if (ArrayUtils.isEmpty(split)) {
            return;
        }

        List<Map<String, Object>> configs = goodsConfigMapper.fetchConfigByAssNumber(sku.getStoreId(), assNumber, split);
        if (configs == null || configs.size() == 0) {
            return;
        }
        Map<String, String> nameMap = new HashMap<>();
        Map<String, Map<String, Object>> specMap = new HashMap<>();
        Map<String, Map<String, Object>> skuMap = new HashMap<>();
        Map<String, String> assortMap = new HashMap<>();
        if (sku.getConfigList() != null && sku.getConfigList().size() > 0) {
            sku.getConfigList().forEach(skuConfig -> assortMap.put(skuConfig.getAttrId(), skuConfig.getValueId()));
        }
        Set<String> attrs = new HashSet<>();
        configs.forEach(e -> {
            genrateSpecListMap(specMap, e, attrs, assortMap);
            genrateSkuListMap(skuMap, e);
        });

        List<Map<String, Object>> skuList = new ArrayList<>(skuMap.values());
        List<Map<String, Object>> specList = new ArrayList<>(specMap.values());
        sku.setSkuList(skuList);
        sku.setSpecList(specList);
    }

    private void genrateSpecListMap(Map<String, Map<String, Object>> specMap, Map<String, Object> e
            , Set<String> attrs, Map<String, String> assortMap) {
        String attrId = (String) e.get("attrId");
        Map<String, Object> spec = specMap.get(attrId);
        List<Map<String, Object>> specValueList = new ArrayList<>();
        if (spec == null) {
            spec = new HashMap<>();
            spec.put("specId", attrId);
            spec.put("title", (String) e.get("configName"));
        } else {
            List<Map<String, Object>> list = (List<Map<String, Object>>) spec.get("specValueList");
            if (list != null) specValueList = list;
        }

        if (attrs.add(attrId + "_" + e.get("valueId"))) {
            Map<String, Object> map = new HashMap<>();
            map.put("skuId", e.get("skuId"));
            map.put("spuId", e.get("spuId"));
            map.put("specValueId", e.get("valueId"));
            map.put("image", e.get("image"));
            map.put("specValue", e.get("configVal") == null ? e.get("valueName") : e.get("configVal"));
            if (StringUtils.equals(assortMap.get(attrId), (String) e.get("valueId"))) {
                map.put("isSelected", true);
            }
            specValueList.add(map);
            spec.put("specValueList", specValueList);
        }

        specMap.put(attrId, spec);
    }

    private void genrateSkuListMap(Map<String, Map<String, Object>> skuMap, Map<String, Object> e) {
        String number = (String) e.get("spuId");
        Map<String, Object> detail = skuMap.get(number);
        List<Map<String, Object>> specInfoList = new ArrayList<>();
        if (detail == null) {
            detail = new HashMap<String, Object>();
            detail.put("skuId", e.get("skuId"));
            detail.put("spuId", e.get("spuId"));
            detail.put("title", e.get("title"));
            detail.put("thumb", e.get("thumb"));
            detail.put("price", e.get("price"));
            detail.put("originPrice", e.get("originPrice"));
            detail.put("stockQuantity", e.get("stockQuantity"));
            detail.put("soldQuantity", e.get("soldNum"));
        } else {
            List<Map<String, Object>> list = (List<Map<String, Object>>) detail.get("specInfo");
            if (list != null) specInfoList = list;
        }

        Map<String, Object> specInfo = new HashMap<>();
        specInfo.put("specId", e.get("attrId"));
        specInfo.put("specTitle", e.get("configName"));
        specInfo.put("specValueId", e.get("valueId"));
        specInfo.put("specValue", e.get("configVal") == null ? e.get("valueName") : e.get("configVal"));

        specInfoList.add(specInfo);
        detail.put("specInfo", specInfoList);
        skuMap.put(number, detail);
    }
}
